package com.renren.http.utils.parser;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.renren.http.beans.User;
import com.renren.http.client.RenRen;
import com.renren.http.utils.StringTookKit;

public class ParserUtils {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		RenRen renren = new RenRen();
		
		renren.login("chenshi5012@163.com", "1qaz2wsx");
		
		User u = new User();
		u.setId(225759382);
		renren.getFriendDetail(u);
		System.out.println(u);
	}

	public List<User> users(String s) {
		return null;
	}

	public static int parserFriendCount(String s) {
		Document doc = Jsoup.parse(s);
		Elements number = doc.body().select("div.sec");

		String text = number.text();
		String tmp = text.substring(text.indexOf('(') + 1, text.indexOf(')'));
		return StringTookKit.String2Integer(tmp);

	}

	public static int parserFriendPage(String s) {
		Document doc = Jsoup.parse(s);
		Elements number = doc.body().select("div.list");
		Element page = number.select("span").first();
		String text = page.text();

		String tmp = text.substring(text.indexOf('/') + 1, text.indexOf("页"));
		return StringTookKit.String2Integer(tmp);

	}

	public static List<Integer> parserFriendId(String s) {

		List<Integer> ids = new ArrayList<Integer>();

		Document doc = Jsoup.parse(s);
		Elements number = doc.body().select("div.list");
		Elements persons = number.select("div");

		for (int i = 1; i < persons.size() - 1; i++) {
			Element e = persons.get(i);
			Element page = e.select("a").first();
			String link = page.attr("href");
			String params = link.substring(link.indexOf('?') + 1,
					link.indexOf('&'));
			String id = params.split("=")[1];
			ids.add(StringTookKit.String2Integer(id));
		}

		return ids;
	}

	/**
	 * 
	 * 解析用户
	 * 
	 */
	public static void parserUser(String s, User user) {
		
		Document doc = Jsoup.parse(s);
		Elements name  = doc.body().select("div.sec");
		user.setName(name.get(1).select("a").text());
		
		Elements msgs = doc.body().select("div.list");
		
		for (Element msg : msgs) {
			
			Elements details = msg.select("div");
			for (Element detail : details) {
				if (detail.attr("class") == "") {
					for (String data : detail.text().split(" ")) {
						if (data.contains("：")) {

							String keyValue[] = data.split("：");

							if (keyValue[0].equals("性别")) {
								user.setSex(keyValue[1]);
							} else if (keyValue[0].equals("生日")) {
								user.setBirth(keyValue[1]);
							} else if (keyValue[0].equals("家乡")) {
								user.setHome(keyValue[1]);
							} else if (keyValue[0].equals("最后登录")) {
								user.setLastLoginDate(keyValue[1]);
							} else if (keyValue[0].equals("兴趣爱好")) {
								user.setHobby(keyValue[1]);
							} else if (keyValue[0].equals("喜欢书籍")) {
								user.setFavoriteBooks(keyValue[1]);
							} else if (keyValue[0].equals("喜欢音乐")) {
								user.setFavoriteMusics(keyValue[1]);
							} else if (keyValue[0].equals("喜欢电影")) {
								user.setFavoriteMovies(keyValue[1]);
							} else if (keyValue[0].equals("喜欢运动")) {
								user.setFavoriteSports(keyValue[1]);
							} else if (keyValue[0].equals("玩的游戏")) {
								user.setFavoriteGames(keyValue[1]);
							} else if (keyValue[0].equals("QQ")) {
								user.setQQ(keyValue[1]);
							} else if (keyValue[0].equals("手机号码")) {
								user.setMobile(keyValue[1]);
							}

						}
					}

				}

			}
		}
	}

}
